package com.ch.light.web.controller;

import com.ch.light.core.Response;
import com.ch.light.core.page.Page;
import com.ch.light.mybatis.ParamFilter;
import com.ch.light.web.aop.annotaion.WebLogger;
import com.ch.light.web.model.DataSourceConfig;
import com.ch.light.web.service.DataSourceConfigService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;


@Controller
@RequestMapping("dataSourceConfig")
public class DataSourceConfigController {

    @Autowired
    private DataSourceConfigService dataSourceConfigService;

    @GetMapping("list")
    public String list(){
        return "dataSourceConfigList";
    }

    @ResponseBody
    @PostMapping("list")
    public Response list(@RequestBody ParamFilter queryFilter){
        List<DataSourceConfig> records = dataSourceConfigService.getList(queryFilter);
        PageInfo<DataSourceConfig> pageInfo = new PageInfo<>(records);
        Page page = queryFilter.getPage();
        page.setResultCount(Long.valueOf(pageInfo.getTotal()).intValue());
        return new Response(records, page);
    }

    @ResponseBody
    @PostMapping("add")
    @WebLogger("添加数据源")
    public Response add(@RequestBody DataSourceConfig record) {
        checkNotNull(record, "用户不能为空");
        dataSourceConfigService.add(record);
        return new Response("添加成功");
    }


    @ResponseBody
    @PostMapping("edit")
    @WebLogger("编辑数据源")
    public Response edit(@RequestBody DataSourceConfig record) {
        dataSourceConfigService.update(record);
        return new Response("修改成功");
    }

    @ResponseBody
    @PostMapping("delete")
    @WebLogger("删除数据源")
    public Response delete(@RequestBody List<String> ids) {
        checkArgument((ids != null && ids.size() > 0), "用户编号不能为空");
        //dataSourceConfigService.delete(ids);
        return new Response("删除成功");
    }

    @ResponseBody
    @PostMapping("detail")
    @WebLogger("查询数据源详细")
    public Response detail(@RequestBody String code) {
        DataSourceConfig record = dataSourceConfigService.findByCode(code);
        return new Response(record);
    }
}
